package com.mxx.bu_yang.dialog;

import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.mxx.bu_yang.R;
import com.yp.loading.base.BaseDialog;

/**
 * 作者：刘江飞
 * 时间：2020/10/15  11:28
 * 邮箱：642371742Z@qq.com
 * 描述：
 **/
public abstract class CommonListDialog<T> extends BaseDialog implements OnItemChildClickListener {
    protected RecyclerView rv_list;
    protected BaseQuickAdapter mAdapter;
    protected BackDataListener<T> backDataListener;

    public void setBackDataListener(@NonNull BackDataListener<T> backDataListener) {
        this.backDataListener = backDataListener;
    }

    public CommonListDialog() {
    }

    public CommonListDialog(int widgetWidth, int location_x, int widgetHeight, int location_y, boolean transparent) {
        super(widgetWidth, location_x, widgetHeight, location_y, transparent);
    }

    @Override
    public boolean isCancelable() {
        return false;
    }

    @Override
    protected boolean isCancelOutside() {
        return false;
    }

    @Override
    protected int getGravity() {
        return Gravity.START | Gravity.TOP;
    }

    @Override
    protected void initView() {
        super.initView();
        rv_list = mRootView.findViewById(R.id.rv_list);
        LinearLayoutManager mManager = new LinearLayoutManager(mActivity);
        rv_list.setLayoutManager(mManager);
        mAdapter = createAdapter();
        if (mAdapter == null) {
            return;
        }
        rv_list.setAdapter(mAdapter);
    }

    protected abstract BaseQuickAdapter createAdapter();

    @Override
    protected void initData() {
        super.initData();
        loadServerData();
    }

    protected abstract void loadServerData();

    @Override
    protected void setListener() {
        super.setListener();
        if (mAdapter == null) {
            return;
        }
        mAdapter.setOnItemChildClickListener(this);
    }

    public interface BackDataListener<T> {
        void backData(T response);
    }
}
